<?php

namespace App\Api\Services;

use App\Api\Models\MaterielTypeModel;
use App\Api\Models\StorageManageModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MaterielTypeService
{
    /**
     * 新建商品信息
     * @param $data
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/4 15:43
     * @return array
     */
    static public function add($data)
    {
        $insertId = MaterielTypeModel::insertGetId($data);
        return ['code' => 0, 'data' => $insertId];
    }

    /**
     * 校验物料商品编号是否存在
     * @param $data
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/4 15:35
     * @return bool
     */
    static public function checkNo($data)
    {
        $checkMaterielNo = MaterielTypeModel::checkMaterielTypeRandCode($data);

        if(!empty($checkMaterielNo)) {
            return false;
        }
        else {
            return true;
        }
    }

    static public function checkRandNo($data)
    {
        $checkMaterielNo = MaterielTypeModel::checkMaterielTypeCode($data);

        if(!empty($checkMaterielNo)) {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * 验证商品信息是否已存在
     * @param $data
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/4 15:41
     * @return bool
     */
    static public function checkMaterielInfo($data)
    {
        $checkMaterielInfo = MaterielTypeModel::checkMaterielInfo($data);
        if(!empty($checkMaterielInfo)) {
            return false;
        }
        else {
            return true;
        }
    }

    //根据id检索商品信息是否已存在
    static public function checkMateriel($mainId, $id)
    {
        if(empty(MaterielTypeModel::where(['id' => $id, 'main_id' => $mainId])->first()))
        {
            return false;
        }
        else {
            return true;
        }
    }

    //检查除自身外是否有相同的商品信息
    static public function checkMaterielInfoExceptSelf($data)
    {
        $checkMaterielTypeInfo = MaterielTypeModel::where([
            'main_id' => $data['main_id'],
            'materiel_name' => $data['materiel_name'],
            'type_name' => $data['type_name'],
            'materiel_class_id' => $data['materiel_class_id'],
            'delete_flag' => 0,
        ])
            ->where('id', '!=', $data['id'])
            ->first();

        if(!empty($checkMaterielTypeInfo)) {
            return false;
        }
        else {
            return true;
        }
    }

    //获取指定商品详细信息
    static public function info($id)
    {
        return MaterielTypeModel::info($id);
    }

    //物料列表
    static public function materielTypeSelect($mainId, $keyword, $condition, $pageSize, $currentPage)
    {
        return MaterielTypeModel::materielTypeSelect($mainId, $keyword, $condition, $pageSize, $currentPage);
    }

    //物料列表-含库存信息
    static public function list($parentId, $data)
    {
        $condition = [];
        if(!empty($data['materiel_class_id'])) {
            $condition['a.materiel_class_id'] = ['EQ' => $data['materiel_class_id']];
        }

        $keyword = empty($data['keyword'])?'':$data['keyword'];

        return MaterielTypeModel::getMaterielTypeList($parentId, $condition, $keyword, $data);
    }

    //修改成本价
    static public function editMaterielCost($data)
    {
        DB::beginTransaction();

        try {
            MaterielTypeModel::where('id', $data['id'])->update(['cost' => $data['cost']]);
            StorageManageModel::where('type_id', $data['id'])->update(['materiel_mean_cost' => $data['cost']]);
            DB::commit();
            return ['code' => 0, 'msg' => 'success', 'data' => ''];
        }
        catch(\Exception $e) {
            DB::rollBack();
            Log::debug('修改成本异常:'.var_export($e->getMessage(), true));
            return ['code' => 1, 'msg' => $e->getMessage(), 'data' => ''];
        }
    }
}
